﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
namespace MusicDeskTop
{
    public partial class TrackForm : Form
    {
        private const string ClassicTrackName = "Классическая";
        private const string PopTrackName = "Попса";
        private const string RockTrackName = "Рок";
        private const string DanceTrackName = "Танцевальная";
        private const string Uverture = "Uverture Track";
        private const string Sonate = "Sonate Track";

        private AbstractTrack track;
        private Dictionary<string, Type> types = new Dictionary<string, Type> ();  
        public TrackForm()
        {
            InitializeComponent();

            types.Add(ClassicTrackName, typeof(ClassicTrack));
            types.Add(PopTrackName, typeof(PopTrack));
            types.Add(RockTrackName, typeof(RockTrack));
            types.Add(DanceTrackName, typeof(DanceTrack));

            foreach (string name in types.Keys)
            {
                typeComboBox.Items.Add(name);
            }
        }
          public void reset() 
            {
            this.classicGenreComboBox.Enabled = false;
            tempComboBox.Enabled = false;
            albumComboBox.Enabled = false;
            severityComboBox.Enabled = false;
            }
        private void typeComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
          
            if (typeComboBox.Text == ClassicTrackName)
            {
                reset();
                this.classicGenreComboBox.Enabled = true;
            }
            if (typeComboBox.Text == DanceTrackName)
            {  
                reset();
                tempComboBox.Enabled = true;
            }
            if (typeComboBox.Text == PopTrackName)
            {
                reset();
                albumComboBox.Enabled = true;
            }
            if (typeComboBox.Text == RockTrackName)
            {
                reset();
                severityComboBox.Enabled = true;
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            int flag;
            Int32.TryParse(this.lengthTextBox.Text, out flag);
            if (flag == 0)
            {
                this.lengthTextBox.Text = "";
                MessageBox.Show("Вводите цифры!");
            }
            if ((nameTextBox.Text == "") ||
                (typeComboBox.Text == "") ||
                (lengthTextBox.Text == ""))
            {
                MessageBox.Show("Заполните все поля");
                return;
            }          
                string typeValue = this.typeComboBox.Text;
                Type type = types[typeValue];

                track = (AbstractTrack)type.GetConstructor(new Type[] { }).Invoke(new object[] { });
             
                int length = Int32.Parse(this.lengthTextBox.Text);
                track.Length = length;

                string name = this.nameTextBox.Text;
                track.Name = name;

                if (typeof(ClassicTrack).IsAssignableFrom(type))
                {
                    ClassicTrack classicTrack = (ClassicTrack)track;
                    if (classicGenreComboBox.Text == Uverture)
                        classicTrack.Type = ClassicTrackType.Uverture;
                    if (classicGenreComboBox.Text == Sonate)
                        classicTrack.Type = ClassicTrackType.Sonate;

                }

                if (typeof(DanceTrack).IsAssignableFrom(type))
                {
                    DanceTrack danceTrack = (DanceTrack)track;
                    danceTrack.Temp = this.tempComboBox.Text;
                }

                if (typeof(RockTrack).IsAssignableFrom(type))
                {
                    RockTrack rockTrack = (RockTrack)track;
                    rockTrack.Severity = this.severityComboBox.Text;
                }

                if (typeof(PopTrack).IsAssignableFrom(type))
                {
                    PopTrack popTrack = (PopTrack)track;
                    popTrack.Album = this.albumComboBox.Text;
                }


                this.Close();
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public AbstractTrack GetTrack()
        {
            return track;
        }
    }
}
